package main;

import java.util.List;
import java.util.Scanner;

import main.util.HibernateContext;
import main.hw1model.*;

public class HW1Demo {
	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);
		boolean dataLoaded = false;
		
		while(true)
		{
			System.out.print("Please type in your command: ");
			String input = in.nextLine();
			String[] command = input.split(" ");
			
			 if (command[0].equalsIgnoreCase("create")) {
	                HibernateContext.createSchema();
	            }
	            else if (command[0].equalsIgnoreCase("load")) {
	        		AirportDesc.load();
	        		AirportCompany.load();
	        		Airline.load();
	        		dataLoaded = true;
	            }
	            else if (command[0].equalsIgnoreCase("add")) {	
	            	
	            }
	            else if (command[0].equalsIgnoreCase("delete")) {
	            }
	            else if (command[0].equalsIgnoreCase("airports")) {
	            }
	            else if (command[0].equalsIgnoreCase("find") &&
	                    (command.length >= 2)) {
	            	if (command[1].equalsIgnoreCase("airportdesc") && command.length == 2) {
	            		List<AirportDesc> airportDescList = AirportDesc.retrieveAll();
	            		System.out.println("ID\torigin\toriginCityName\toriginStateAbr\tOrigin_Airport_Id");
	            		for (AirportDesc ad : airportDescList) {
	            			System.out.println(ad.getDescId() + "\t"
	            								+ ad.getOrigin() + "\t"
	            								+ ad.getOriginCityName() + "\t"
	            								+ ad.getOriginStateAbr() + "\t\t"
	            								+ ad.getAirport().getId());
	            		}
	            	} else if (command[1].equalsIgnoreCase("airport")) {
	            		Airport airport = Airport.find("San Francisco Interantional Airport");
	            		System.out.println("id\tname");
	            		System.out.println(airport.getId() + "\t" + airport.getName());
	            	} else if (command.length == 3 && command[1].equalsIgnoreCase("airportdesc")) {
	            		AirportDesc airportDesc = AirportDesc.find(command[2]);
	            		System.out.println("ID\torigin\toriginCityName\toriginStateAbr\tOrigin_Airport_Id");
	            		System.out.println(airportDesc.getDescId() + "\t"
								+ airportDesc.getOrigin() + "\t"
								+ airportDesc.getOriginCityName() + "\t\t"
								+ airportDesc.getOriginStateAbr() + "\t\t"
								+ airportDesc.getAirport().getId());
	            		
	            	}
	            }
	            else if (command[0].equalsIgnoreCase("airportcompany") && 
	            		(command.length == 2)) {
	            	AirportCompany company = AirportCompany.find(command[1]);
	            	if(company != null) {
	            		System.out.println("ID\tABR");
	            		company.printInSession();
	            	}
	            	else {
	            		System.out.println("No such company.");
	            	}
	            }
	            else if (command[0].equalsIgnoreCase("companies") && 
	            		(command.length == 1)) {
	            	AirportCompany.list();
	            }
	            else if (command[0].equalsIgnoreCase("airline")) {
	            	List<Airline> airlines = Airline.retrieveAll();
	            	if(airlines != null) {
	            		System.out.println("ID\tFLIGHTNUMBER\tORIGIN\tDESTINATION\tAIRPORT");
	            		for(Airline air : airlines) {
	            			air.printInSession();
	            		}
	            	}
	            }
	            else if (command[0].equalsIgnoreCase("quit")) {
	            	System.out.println("\nSystem Exit");
	            	if (dataLoaded)
	            		HibernateContext.closeSessionFactory();
	            	break; 
	            }
		}
	}
}
